﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using HYElectricKernel;
using HYRElectric.WirePlace;

namespace HYRElectric.Equipment
{
	public class ArcLineLayout : EquipmentLayout
	{
		public ArcLineLayout(UIDocument uiDoc, Document document, DbFamilyObject familyObject, FamilySymbol selectedFamilySymbol, List<BuiltInCategory> lstFilter) : base(uiDoc, document, familyObject, selectedFamilySymbol, lstFilter)
		{
		}

		public override bool Layout()
		{
			if (!base.Layout())
			{
				return false;
			}
			this.form = new ArcLineLayoutForm(this.doc);
			this.form.Show(WindowHelper.HWndRevit);
			WindowHelper.SendMessageToWndRevit();
			for (;;)
			{
				try
				{
					this.startPoint = this.docUI.Selection.PickPoint("请选取圆弧起点");
					this.midPoint = this.docUI.Selection.PickPoint("请选取圆弧中间任意一点");
					this.endPoint = this.docUI.Selection.PickPoint("请选取圆弧终点");
					if ((this.midPoint - this.startPoint).IsZeroLength() || (this.endPoint - this.startPoint).IsZeroLength() || (this.endPoint - this.midPoint).IsZeroLength())
					{
						TaskDialog.Show("盈建科提示", "三点不能重合，请选取三个不同点。");
						continue;
					}
					XYZ xyz = (this.midPoint - this.startPoint).Normalize();
					XYZ xyz2 = (this.endPoint - this.midPoint).Normalize();
					if (xyz.IsAlmostEqualTo(xyz2) || xyz.Negate().IsAlmostEqualTo(xyz2))
					{
						TaskDialog.Show("盈建科提示", "三点不能共线。");
						continue;
					}
				}
				catch (Exception)
				{
					break;
				}
				this.centerPoint = null;
				Dictionary<XYZ, double> insertPoints = this.GetInsertPoints();
				if (insertPoints != null && insertPoints.Count > 0)
				{
					TransactionGroup transactionGroup = new TransactionGroup(this.doc, "TransactionGroup");
					try
					{
						transactionGroup.Start();
						FamilyInstance familyInstance = null;
						foreach (KeyValuePair<XYZ, double> keyValuePair in insertPoints)
						{
							Transaction transaction = null;
							try
							{
								transaction = new Transaction(this.doc, "ArcLineLayout");
								transaction.Start();
								FamilyInstance familyInstance2 = YJKRevitTools.LayFamilySymbol(this.doc, keyValuePair.Key, this.familySymbol, this.doc.ActiveView.GenLevel, keyValuePair.Value);
								if (familyInstance2 == null)
								{
									throw new Exception("布置设备出错。");
								}
								base.SetParametersValue(familyInstance2, this.dbFamilyObject.Size);
								transaction.Commit();
								if (this.form.IsLink && familyInstance != null)
								{
                                    NewConnectWire.CreateWire(this.doc, familyInstance, familyInstance2, (Autodesk.Revit.DB.Electrical.WiringType)0, this.centerPoint);
								}
								familyInstance = familyInstance2;
							}
							catch (Exception)
							{
								if (transaction != null && transaction.GetStatus() != TransactionStatus.Committed)
								{
									transaction.RollBack();
								}
								break;
							}
						}
						transactionGroup.Assimilate();
						continue;
					}
					catch (Exception)
					{
						transactionGroup.RollBack();
						continue;
					}
					break;
				}
			}
			this.form.Close();
			return true;
		}

		private Dictionary<XYZ, double> GetInsertPoints()
		{
			if (this.form == null)
			{
				return null;
			}
			Dictionary<XYZ, double> dictionary = new Dictionary<XYZ, double>();
			int count = this.form.Count;
			double margin = this.form.Margin;
			bool isAutoAngle = this.form.IsAutoAngle;
			int angle = this.form.Angle;
			Arc arc = this.doc.Application.CreatYJKArc(this.startPoint, this.endPoint, this.midPoint);
			XYZ normal = arc.Normal;
			this.centerPoint = arc.Center;
			XYZ pt = (normal.Z < 0.0) ? this.endPoint : this.startPoint;
			double num = Math.Abs(arc.GetYJKEndParameter(1) - arc.GetYJKEndParameter(0));
			double num2 = (Math.Abs(2.0 * margin + (double)count - 1.0) < 1E-06) ? 0.0 : (num / (2.0 * margin + (double)count - 1.0));
			for (int i = 0; i < count; i++)
			{
				XYZ xyz = (num2 < 1E-06) ? this.startPoint : Geometry.RotateTo(pt, this.centerPoint, XYZ.BasisZ, (margin + (double)i) * num2);
				double num3 = XYZ.BasisX.AngleOnPlaneTo(xyz - this.centerPoint, XYZ.BasisZ);
				num3 = (isAutoAngle ? (num3 - Math.PI*.5) : ((double)angle));
				num3 = AssistFunc.RadianToAngle(num3);
				dictionary.Add(xyz, num3);
			}
			return dictionary;
		}

		private ArcLineLayoutForm form;

		private XYZ startPoint;

		private XYZ midPoint;

		private XYZ endPoint;

		private XYZ centerPoint;
	}
}
